<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/28
 * Time: 16:12
 */

namespace app\admin\controller;
use think\Config;
use think\Controller;
use think\Db;
use think\Request;
use Common\Oss;

class Setting extends Common
{

    /**
     * 协议编辑 type 1表示用户协议  2表示商家协议 3功能介绍 4关于我们
     * @return \think\response\Json
     */
    public function addUserProtocol()
    {
        $data = Request::instance()->post();
        if(array_key_exists('content' , $data) && array_key_exists('type' , $data))
        {
            if(array_key_exists('id' , $data))
            {
                $save_data['content'] = $data['content'];
                $save_data['create_time'] = date('Y-m-d H:i:s');
                $status = Db::name('protocol')->where('id',$data['id'])->update($save_data);
                if($status)
                {
                    $result = Config::get('code.success');
                }
                else
                {
                    $result = Config::get('code.error');
                }
            }
            else
            {
                $ins_data['content'] = $data['content'];
                $ins_data['type'] = $data['type'];
                $ins_data['create_time'] = date('Y-m-d H:i:s');
                $status = Db::name('protocol')->insert($ins_data);
                if($status)
                {
                    $result = Config::get('code.success');
                }
                else
                {
                    $result = Config::get('code.error');
                }
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 获取协议内容
     * @return \think\response\Json
     */
    public function getProtocolInfo()
    {
        $type = input('post.type');
        if($type)
        {
            $info = Db::name('protocol')->where('type',$type)->find();
            if($info)
            {
                $result = Config::get('code.success');
                $result['data'] = $info;
            }
            else
            {
                $result = Config::get('code.null');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }


    /**
     * 获取系统配置信息
     * @return \think\response\Json
     */
    public function getSettingInfo()
    {
        $info = Db::name('system_setting')->select();
        if($info)
        {
            foreach ($info as $key => $value)
            {
                $info[$key]['json_str'] = 'id:'.$value['id'].',name:'.$value['name'].',value:'.$value['value'];
            }
            $result = Config::get('code.success');
            $result['data'] = $info;
        }
        else
        {
            $result = Config::get('code.null');
        }
        return json($result);
    }

    /**
     * 系统设置上传图片
     * @return \think\response\Json
     */
    public function uploadSettingImg()
    {
        $file = $_FILES;
        $file_name = array_keys($file);
        if($file)
        {
            $ossClient = new Oss();
            $files     = $ossClient->uploadFile('banner', $file_name[0]);
            if($files)
            {
                $status = '';
                $ins_data['value'] = join(",",$files);
                if($file_name[0] == 'supplier_android_image')
                {
                    $status = Db::name('system_setting')->where('id' , 12)->update($ins_data);
                }
                else if($file_name[0] == 'user_android_image')
                {
                    $status = Db::name('system_setting')->where('id' , 11)->update($ins_data);
                }
                else if($file_name[0] == 'user_ios_image')
                {
                    $status = Db::name('system_setting')->where('id' , 13)->update($ins_data);
                }
                else if($file_name[0] == 'supplier_ios_image')
                {
                    $status = Db::name('system_setting')->where('id' , 14)->update($ins_data);
                }
                if($status)
                {
                    $result = Config::get('code.success');
                    $result['img_path'] = $ins_data['value'];
                }
                else
                {
                    $result = Config::get('code.error');
                }
            }
            else
            {
                $result = Config::get('code.error');
            }
        }
        return json($result);
    }

    public function setting()
    {
        $data = Request::instance()->post();
        if($data)
        {
            foreach ($data['data'] as $key => $value)
            {
                $res = explode(',',$value);
                $arr1 = explode(':',$res[0]);
                $id = $arr1[1];
                $arr2 = explode(':',$res[1]);
                $save_data = [];
                $save_data['value'] = $arr2[1];
                Db::name('system_setting')->where('id',$id)->update($save_data);
            }
            $result = Config::get('code.success');
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

}
